/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.HTMLUIcomponents;

import java.util.ArrayList;

import model.DHTMLConstants;
import model.Labels;

import org.openrdf.model.URI;

import controller.HelperFunctions;
import controller.elementMapper.ClassMapper;
import controller.elementMapper.Mapper;
import controller.elementMapper.PropertyMapper;


public class BroaderThan extends AbstractComponent {
	

	private static final String CSSIDFIELD = "divBroaderThan";
	public static final String CSSIDCONTENT = "divBroaderThancontent";
	public static final String NAMEFORMELEMENT = "BroaderThanEntry";
	
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInViewMode( URI uri, String viewType, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {
		String ret = "";

		model.Label l = null;
		if( viewType.equals( DHTMLConstants.CLASS_VIEWTYPE ))
			l = Labels.BROADERTHANCLASSES;
		else if( viewType.equals( DHTMLConstants.PROPERTY_VIEWTYPE ))
			l = Labels.BROADERTHANPROPERTIES;
		
		ret += getTitleContainer( l, viewType, false, true );
		ret += getFormContainer( getFormInViewMode( uri, viewType, showNavigation, showNavigationInEditMode ), CSSIDCONTENT );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInEditMode( URI uri, String viewType, ArrayList<URI> entryList, ArrayList<Exception> exceptions, boolean showNavigation, boolean showCreateLinks ) throws Exception {
		String ret = "";

		model.Label l = null;
		if( viewType.equals( DHTMLConstants.CLASS_VIEWTYPE ))
			l = Labels.BROADERTHANCLASSES;
		else if( viewType.equals( DHTMLConstants.PROPERTY_VIEWTYPE ))
			l = Labels.BROADERTHANPROPERTIES;
		
		ret += getTitleContainer( l, viewType, false, true );
		ret += getFormContainer( getFormInEditMode( uri, viewType, entryList, exceptions, showNavigation, showCreateLinks ), CSSIDCONTENT );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	
	public static String getFormInViewMode( URI uri, String viewType, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {

		String ret = "";	

		java.util.ArrayList<URI> list = null;
		if( viewType.equals( DHTMLConstants.CLASS_VIEWTYPE ) ) {
			
			list = ClassMapper.selectBroaderThan( uri );
		}
		else if( viewType.equals( DHTMLConstants.PROPERTY_VIEWTYPE ) ) {
			
			list = PropertyMapper.selectBroaderThan( uri );
		}
		String content = getElementsHTMLList( list );	

		ret = renderViewForm( 
			content, 
			Mapper.selectHasVersionDateTimeString(uri),
			CSSIDCONTENT, 
			showNavigation, 
			showNavigationInEditMode,
			true,
			viewType );
		
		return ret;
	}	

	
	public static String getFormInEditMode( URI uri, String viewType, ArrayList<URI> entryList, ArrayList<Exception> exceptions, boolean showNavigation, boolean showCreateLinks ) throws Exception {
		
		String ret = "";

		String actualVersionContent = NOTHINGSELECTED;
		String actualTimestamp = Mapper.selectHasVersionDateTimeString(uri);
		model.Label l = null;
		
		if( uri!=null ) {

			java.util.ArrayList<URI> list = null;
			if( viewType.equals( DHTMLConstants.CLASS_VIEWTYPE ) ) {
				
				list = ClassMapper.selectBroaderThan( uri );
				l = Labels.BROADERTHANCLASSES;
			}
			else if( viewType.equals( DHTMLConstants.PROPERTY_VIEWTYPE ) ) {
				
				list = PropertyMapper.selectBroaderThan( uri );
				l = Labels.BROADERTHANPROPERTIES;
			}
			
			if( list!=null ) {
				actualVersionContent = getElementsHTMLList( list );
			}
		}
				
		// String content = getSelectBoxReduceableByOM( NAMEFORMELEMENT, entry, DHTMLConstants.CLASSESBELONGINGTOOM );

		String createNewAdditionalParams = NAMEFORMELEMENT + "=" + HelperFunctions.getUriForHref( uri );
		
		String content = getItemsEditPanel( NAMEFORMELEMENT, entryList, viewType, createNewAdditionalParams, showCreateLinks );
		
		ret = renderEditForm( 
				l, 
				content,
				exceptions, 
				actualVersionContent, 
				actualTimestamp,
				CSSIDCONTENT, 
				NAMEFORMELEMENT, 
				showNavigation );


		return ret;
	}	

}
